package com.abdu.demo.kotlindemo

import android.animation.ObjectAnimator
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.CheckBox
import android.widget.EditText
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import java.util.Timer
import java.util.TimerTask

class RegisterActivity : AppCompatActivity() {
    private var context: Context = this
    private lateinit var tv_loginError: TextView
    private lateinit var lv_loginBox: LinearLayout
    private lateinit var et_account: EditText
    private lateinit var et_password: EditText
    private lateinit var et_password2: EditText
    private lateinit var btn_login: ImageView
    private lateinit var btn_register: Button

    private var account: String = ""
    private var password: String = ""
    private var password2: String = ""

    private lateinit var dbHelper: DatabaseHelper

    private lateinit var objectAnimator: ObjectAnimator

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_register)

        initView()
        register()
    }



    private fun initView() {
        tv_loginError = findViewById(R.id.tv_loginError)
        lv_loginBox = findViewById(R.id.lv_LoginBox)
        et_account = findViewById(R.id.et_account)
        et_password = findViewById(R.id.et_password)
        et_password2 = findViewById(R.id.et_password2)
        btn_register = findViewById(R.id.btn_register)
        btn_login = findViewById(R.id.btn_login)
        btn_login.setOnClickListener{
            startActivity(Intent(context,loginActivity::class.java))
            finish()
        }
    }

    private fun register() {
        dbHelper = DatabaseHelper(this)

        btn_register.setOnClickListener{
            account = et_account.text.toString()
            password = et_password.text.toString()
            password2 = et_password2.text.toString()


            if (account.length==0 || password.length==0 || password2.length==0 ){
                loginError("请规范输入用户名和密码")
                return@setOnClickListener
            }


            if (account.length<4 ){
                loginError("用户名至少输入4个字符")
                return@setOnClickListener
            }


            if ( password.length<6 ){
                loginError("密码至少输入6个字符")
                return@setOnClickListener
            }
            if (password!= password2){
                loginError("两次密码不一致！")
                return@setOnClickListener
            }

            if (!dbHelper.checkUsername(account)){
                val isInserted = dbHelper.addUser(account, password)
                if (isInserted) {
                    showToast("注册成功！")
                    tv_loginError.text = ""
                    Timer().schedule(object : TimerTask() {
                        override fun run() {
                            runOnUiThread {
                                startActivity(Intent(context,loginActivity::class.java))
                                finish()
                            }
                        }
                    }, 500)

                } else {
                    showToast("注册失败！")
                    return@setOnClickListener
                }
            } else {
                loginError("用户名已存在！")
                return@setOnClickListener
            }
        }
    }

    private fun showToast(text: String){
        Toast.makeText(context, text, Toast.LENGTH_SHORT).show()
    }

    private fun loginError(msg: String){
        tv_loginError.text = msg
        objectAnimator = ObjectAnimator.ofFloat(tv_loginError,
            "translationX", 0f, 50f, -50f, 0f)
        objectAnimator.duration = 850
        objectAnimator.repeatCount = 0
        objectAnimator.start()

        objectAnimator = ObjectAnimator.ofFloat(lv_loginBox,
            "translationX", 0f, 30f, -30f, 0f)
        objectAnimator.duration = 700
        objectAnimator.start()
    }

}